package nccloud.web.hrpbm.budgetswap.budgetswapvo.action;

import nc.itf.org.IOrgResourceCodeConst;
import nccloud.commons.lang.ArrayUtils;
import nccloud.framework.core.model.entity.VOStatus;
import nccloud.framework.web.convert.translate.Translator;
import nccloud.framework.web.processor.IRefSqlBuilder;
import nccloud.framework.web.processor.refgrid.RefQueryInfo;
import nccloud.framework.web.ui.meta.RefMeta;
import nccloud.framework.web.ui.model.GridModel;
import nccloud.framework.web.ui.model.refgrid.RefRow;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.model.row.Row;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.refer.DefaultGridRefAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预算号参照
 * @author Aden
 * @date 2024-02-28
 */
public class BudgetNoDefaultGridRefAction extends DefaultGridRefAction {

    public BudgetNoDefaultGridRefAction() {
        super();
        setResourceCode(IOrgResourceCodeConst.ORG);
    }

    @Override
    public RefMeta getRefMeta(RefQueryInfo refQueryInfo) {
        // 定义查询字段 项目名称和收支项目
        refQueryInfo.setDefineItems(new String[]{"project_name","inoutbusiclass_name"});

        RefMeta refMeta = new RefMeta();
        setResourceCode(IOrgResourceCodeConst.ORG);
        refMeta.setCodeField("dimno");
        refMeta.setNameField("dimno");

        refMeta.setPkField("pk_busictrldata");
        refMeta.setTableName("(select m.*,p.project_name,i.name as inoutbusiclass_name from hrpbctp_busictrldata m "
                + " left join bd_project p on m.dim3 = p.pk_project"
                + " left join bd_inoutbusiclass i on m.dim4 = i.PK_INOUTBUSICLASS) hrpbctp_busictrldata");
        refMeta.setMutilLangNameRef(false);
        refMeta.setExtraFields(new String[]{
                "pk_busictrldata","dimno","dim2","dim3","dim4","dim5","defdim1","defdim2","defdim3","defdim4","defdim5","defdim6","defdim7","defdim8","defdim9","defdim10","freezeamount","balance","controlamount","adjustamount"
        });

        return refMeta;
    }

    @Override
    protected RefRow[] processRefRows(RefQueryInfo refQueryInfo, IRefSqlBuilder refSqlBuilder, RefRow... refRows) {
        RefRow[] superRefRows = super.processRefRows(refQueryInfo, refSqlBuilder, refRows);
        // 模板翻译
        if(ArrayUtils.isNotEmpty(superRefRows)) {
            // 转换为Grid
            Grid grid = new Grid();
            grid.setPageid("M0265505_BudgetswapVO");
            grid.setTempletid("1001YL1LCP0000000RL2");
            grid.setModel(new GridModel());
            grid.getModel().setAreacode("BudgetNoReferAreaCode");
            Map<String, RefRow> refRowMap = new HashMap<>(superRefRows.length);
            List<Row> rowList = new ArrayList<>(superRefRows.length);
            for (RefRow superRefRow : superRefRows) {
                rowList.add(convertRow(superRefRow));
                refRowMap.put(superRefRow.getRefpk(), superRefRow);
            }
            grid.getModel().add(rowList);
            Translator translator = new Translator();
            translator.translate(grid);
            Row[] rows = grid.getModel().getRows();
            if(ArrayUtils.isNotEmpty(rows)) {
                for (Row row : rows) {
                    Object controlamount = row.getValues().get("controlamount").getValue();
                    if(controlamount != null && Double.valueOf(controlamount.toString()) > 0) {
                        RefRow refRow = refRowMap.get(row.getPrimaryKey());
                        if(refRow != null) {
                            Map<String, Cell> translateCellMap = row.getValues();
                            for (Map.Entry<String,Cell> entry : translateCellMap.entrySet()) {
                                refRow.addCell(entry.getKey(), entry.getValue());
                            }
                        }
                    }
                }
            }
        }
        return superRefRows;
    }

    private Row convertRow(RefRow refRow) {
        Row row = new Row();
        row.setStatus(VOStatus.Origin);
        if(refRow != null) {
            row.setPrimaryKey(refRow.getRefpk());
            row.setRowid(refRow.getRefpk());
            Map<String, Cell> cellMap = refRow.getValues();
            for (Map.Entry<String,Cell> entry : cellMap.entrySet()) {
                row.addCell(entry.getKey(),entry.getValue());
            }
        }
        return row;
    }

    @Override
    public String getExtraSql(RefQueryInfo refQueryInfo, RefMeta refMeta) {
        return " and nvl(controlamount,0) >= 0  ";
    }
}
